﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Resources;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;

public class FileManager{
	public enum enFileOperation
	{
		ReadFile,
		WriteFile,
		DeleteFile,
		CreateDirectory,
		DeleteDirectory,
	}

	public delegate void DelegateOnOperateFileFail(string fullpath, enFileOperation operation, Exception ex);

	private static string s_cachepath = null;
	private static string s_extractFloder = "Resources";
	private static string s_ectractPath = null;
	private static MD5CryptoServiceProvider s_md5Provider = new MD5CryptoServiceProvider();
	public static FileManager.DelegateOnOperateFileFail s_deldgateOnOperateFileFail = delegate(string fullpath, enFileOperation operation, Exception exception) {  };

	public static bool IsFileExist(string filePath)
	{
		return File.Exists(filePath);
	}

	public static bool IsDirectoryExist(string directory)
	{
		return Directory.Exists(directory);
	}
	public static bool CreateDirectory(string directory)
	{
		if (IsDirectoryExist(directory))
		{
			return true;
		}
		bool result;
		try
		{
			Directory.CreateDirectory(directory);
			result = true;
		}
		catch (Exception e)
		{
			result = false;
			s_deldgateOnOperateFileFail(directory, enFileOperation.CreateDirectory, e);
		}
		return result;
	}

	public static bool DelectDirectory(string directory)
	{
		if (!IsDirectoryExist(directory))
		{
			return true;
		}
		bool result;
		try
		{
			Directory.Delete(directory);
			result = true;
		}
		catch (Exception e)
		{
			result = false;
			s_deldgateOnOperateFileFail(directory, enFileOperation.DeleteDirectory, e);
		}
		return result;
	}

	public static int GetFileLength(string filepath)
	{
		if (!IsFileExist(filepath))
		{
			return 0;
		}
		int result;
		try
		{
			FileInfo fileInfo = new FileInfo(filepath);
			result = (int)fileInfo.Length;
		}
		catch (Exception e)
		{
			result = 0;
		}
		return result;
	}

	public static byte[] ReadFile(string filepath)
	{
		if (!IsFileExist(filepath))
		{
			return null;
		}
		byte[] result = null;
		try
		{
			result = File.ReadAllBytes(filepath);
		}
		catch (Exception e)
		{
			s_deldgateOnOperateFileFail(filepath, enFileOperation.ReadFile, e);
		}
		return result;
	}

	public static bool WriteFile(string filepath, byte[] data)
	{
		bool result;
		try
		{
			File.WriteAllBytes(filepath,data);
			result = true;
		}
		catch (Exception e)
		{
			result = false;
			s_deldgateOnOperateFileFail(filepath, enFileOperation.WriteFile, e);
		}
		return result;
	}

	public static bool WriteFile(string filepath, byte[]data, int offect, int length)
	{
		bool result;
		FileStream file = null;
		try
		{
			file = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
			file.Write(data,offect,length);
			file.Close();
			result = true;
		}
		catch (Exception e)
		{
			if (file != null)
			{
				file.Dispose();
			}
			result = false;
			DeleteFile(filepath);
			s_deldgateOnOperateFileFail(filepath, enFileOperation.WriteFile, e);
		}
		return result;
	}

	public static bool DeleteFile(string filename)
	{
		if (!IsFileExist(filename))
		{
			return true;
		}
		bool result;
		try
		{
			result = true;
		}
		catch (Exception e)
		{
			result = false;
			s_deldgateOnOperateFileFail(filename, enFileOperation.DeleteFile, e);
		}
		return result;
	}

	public static void CopyFile(string srcpath, string desfile)
	{
		File.Copy(srcpath,desfile,true);
	}

	public static string GetFileMD5(string filepath)
	{
		if (!IsFileExist(filepath))
		{
			return string.Empty;
		}
		string result;
		try
		{
			result = BitConverter.ToString(s_md5Provider.ComputeHash(ReadFile(filepath))).Replace("-", string.Empty);
		}
		catch (Exception e)
		{
			result = string.Empty;
		}
		return result;
	}

	public static string GetMD5(byte[] data)
	{
		return BitConverter.ToString(s_md5Provider.ComputeHash(data)).Replace("-", string.Empty);
	}

	public static string GetMD5(string str)
	{
		return BitConverter.ToString(s_md5Provider.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", string.Empty);
	}

	public static string CombinePath(string path1, string path2)
	{
		if (path1.LastIndexOf("/", StringComparison.Ordinal) != path1.Length - 1)
		{
			path1 = path1 + "/";
		}//在 保证path1后面有"/"
		if (path2.IndexOf("/", StringComparison.Ordinal) == 0)
		{
			path2 = path2.Substring(1);
		}// 保证path2 第一位没有"/"
		return path1 + path2;
	}

	public static string CombinePath(params string[] parms)
	{
		if (parms.Length < 0)
		{
			return string.Empty;
		}
		if (parms.Length == 1)
		{
			return CombinePath(parms[0], string.Empty);
		}
		string str = CombinePath(parms[0], parms[1]);
		for (int i = 2; i < parms.Length; i++)
		{
			str = CombinePath(str, parms[i]);
		}
		return str;
	}

	public static string GetStreamingAssetsPathWithHeader(string filename)
	{
		return CombinePath(Application.streamingAssetsPath, filename);
	}

	public static string GetCachePath()
	{
		if (s_cachepath == null)
		{
			s_cachepath = Application.persistentDataPath;
		}
		return s_cachepath;
	}

	public static string GetCachePath(string filename)
	{
		return CombinePath(GetCachePath(), filename);
	}
	
	public static string GetCachePathWithHeader(string filename)
	{
		return CombinePath(GetLocalPathHandle(),GetCachePath(filename));
	}

	public static string GetExtractPath()
	{
		if (s_ectractPath == null)
		{
			s_ectractPath = CombinePath(GetCachePath(), s_extractFloder);
		}
		return s_ectractPath;
	}

	public static string GetFullName(string fullpath)
	{
		if (fullpath == null)
		{
			return null;
		}
		int num = fullpath.LastIndexOf("/", StringComparison.Ordinal);
		if (num > 0)
		{
			return fullpath.Substring(num + 1, fullpath.Length - num - 1);
		}
		return fullpath;
	}

	public static string EraseExtension(string fullname)
	{
		if (fullname == null)
		{
			return null;
		}
		int num = fullname.LastIndexOf(".", StringComparison.Ordinal);
		if (num > 0)
		{
			return fullname.Substring(0, num);
		}
		return null;
	}

	public static string GetExtension(string fullname)
	{
		if (fullname == null)
		{
			return null;
		}
		int num = fullname.LastIndexOf(".", StringComparison.Ordinal);
		if (num > 0 && num+1<fullname.Length)
		{
			return fullname.Substring(num);
		}
		return null;
	}

	public static string GetFullDirectory(string fullname)
	{
		return Path.GetDirectoryName(fullname);
	}

	public static bool ClearDirectory(string fullpath)
	{
		bool result;
		try
		{
			string[] file = Directory.GetFiles(fullpath);
			for (int i = 0; i < file.Length; i++)
			{
				File.Delete(file[i]);
			}
			string[] dirs = Directory.GetDirectories(fullpath);
			for (int i = 0; i < dirs.Length; i++)
			{
				Directory.Delete(dirs[i],true);
			}
			result = true;
		}
		catch (Exception e)
		{
			result = false;
		}
		return result;
	}

	private static string GetLocalPathHandle()
	{
		return "file://";
	}

	public static bool ClearDirectpry(string fullpath, string[] fileextFilter, string[] folderFilter)
	{
		bool result;
		try
		{
			if (fileextFilter != null)
			{
				string[] files = Directory.GetFiles(fullpath);
				for (int i = 0; i < files.Length; i++)
				{
					for (int j = 0; j < fileextFilter.Length; j++)
					{
						if (files[i].Contains(fileextFilter[j]))
						{
							DeleteFile(files[i]);
							break;
						}
					}
				}
			}
			if (folderFilter != null)
			{
				string[] dics = Directory.GetDirectories(fullpath);
				for (int i = 0; i < dics.Length; i++)
				{
					for (int j = 0; j < folderFilter.Length; j++)
					{
						if (dics[i].Contains(folderFilter[j]))
						{
							Directory.Delete(dics[i]);
							break;
						}
					}
				}
			}
			result = true;
		}
		catch (Exception e)
		{
			result = false;
		}
		return result;
	}
}
